﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using web.core.ViewModels;
using web.core.Models;
using web.component;

namespace web.core.Controllers
{
    public class admPartnerController : baseController
    {
        //------------------------------------
        //partner_list PAGE
        //------------------------------------
        #region partner_list PAGE
        public ActionResult partner_list()
        {
            bool permission = false;
            InitPage(ref permission);
            ViewBag.permission = permission;
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult ctr_ct_list(int? pageindex)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            return ctr_ct_list(pageindex, admin_const.PAGE_LIST_SIZE, languageId);
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult ctr_ct_list(int? pageindex, int? pagesize, int? languageId)
        {
            var lstdelete = Request.Form["is_delete"];
            if (!string.IsNullOrEmpty(lstdelete)) {
                var arr = lstdelete.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach(var iterator in arr){
                    if (iterator != "false")
                        partnerModels.deleteItem(Convert.ToInt16(iterator));
                }
            } 
            var page_view = new ViewModels.PartnerPageListViewModels()
            { 
                //list_group = accountModels.getListUserGroup(),
                list_pagesize = GetSizePagingPublic(pagesize ?? admin_const.PAGE_LIST_SIZE),
                pagesize = pagesize ?? admin_const.PAGE_LIST_SIZE
            };
            TryUpdateModel(page_view);
            if (page_view.languageId != null)
            {
                languageId = page_view.languageId;
            }
            var langid = (languageId ?? admin_const.DEFAULT_LANGUAGE_ID);

            var lst_lang = languageModels.get_list_language();
            page_view.select_list_language = new SelectList(lst_lang, "language_id", "language_desc");

            page_view.page_list_partner = Models.partnerModels.adm_partner_plist(pageindex ?? 1, pagesize ?? admin_const.PAGE_LIST_SIZE, langid);
            return PartialView("../Shared/partner/list_partner", page_view);
        }

        public ActionResult updateStatus(string id)
        {
            int pid = int.Parse(id.Split(',')[0]);
            var sid = int.Parse(id.Split(',')[1]);
            bool isv = (sid == 0 ? false : true);
            partnerModels.updateStatus(pid, isv);
            string outStr = "" + sid.ToString();
            return Json(outStr);
        }
        #endregion

        //------------------------------------
        //partner_edit PAGE
        //------------------------------------
        #region partner_edit PAGE
        public ActionResult partner_edit()
        {
            bool permission = false;
            InitPage(ref permission);
            ViewBag.permission = permission;
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult ctr_ct_edit(int? id)
        {
            int itemid = id ?? 0;
            if (itemid == 0)
                ViewBag.isAdd = true;
            else
                ViewBag.isAdd = false;

            ViewModels.PartnerViewModels item_view;
            //var item_view = new ViewModels.LanguageViewModels();
            var it = partnerModels.getByID(itemid);
            if (it != null)
            {
                string sUrl = siteSettingSingleModels.GetConfig(global_const.WEBSITE_URL);
                item_view = new PartnerViewModels()
                {
                    partner_id = it.partner_id,
                    partner_name = it.partner_name,
                    short_information = it.short_information,
                    more_information = it.more_information,
                    image_path1 = it.image_path1,
                    image_path2 = it.image_path2,
                    link = it.link,
                    date_modified = it.date_modified,
                    language_id = it.language_id ?? admin_const.DEFAULT_LANGUAGE_ID,
                    order_display = it.order_display,
                    keyword = it.keyword,
                    description = it.description,
                    is_vip = it.is_vip,

                    image_display1 = Ufile.LoadImage(sUrl, it.image_path1),
                    image_display2 = Ufile.LoadImage(sUrl, it.image_path2)
                };
            }
            else
                item_view = new PartnerViewModels()
                {
                    is_vip =  admin_const.DEFAULT_ITEM_STATUS_B,
                    language_id = admin_const.DEFAULT_LANGUAGE_ID,
                    order_display = admin_const.DEFAULT_ORDER_DISPLAY,
                    //listLanguage = languageModels.get_list_language()
                };

            var lst_lang = languageModels.get_list_language();
            item_view.select_list_language = new SelectList(lst_lang, "language_id", "language_desc", item_view.language_id);
            item_view.select_list_status = new List<SelectListItem>() {  
                    new SelectListItem{Selected=item_view.is_vip == true?true:false, Text = "Vip", Value = "true" },
                    new SelectListItem{Selected=item_view.is_vip == true?true:false, Text = "ko Vip", Value = "false" }
            };

            return PartialView("../Shared/partner/edit_partner", item_view);
        }

        [ChildActionOnly]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult ctr_ct_edit(FormCollection collection)
        {
            var item_view = new ViewModels.PartnerViewModels();
            TryUpdateModel(item_view);

            if (string.IsNullOrEmpty(item_view.partner_name) )
            {
                ViewBag.result_message = "Tên đối tác không để trống.";
                ViewBag.result = false;
                return PartialView("../Shared/partner/edit_partner", item_view);
            }
            bool isdel = item_view.partner_id != 0 ? true : false;
            var item = new data.entities.partners()
            {
                partner_id = item_view.partner_id,
                partner_name = item_view.partner_name,
                short_information = item_view.short_information,
                more_information = item_view.more_information,
                image_path1 = SaveImage(isdel, item_view.image_path1, item_view.fileUpload1,admin_const.E_IMG_TYPE.PARTNER, admin_const.E_IMG_THUMB.NO_THUMB),
                image_path2 = SaveImage(isdel, item_view.image_path2, item_view.fileUpload2, admin_const.E_IMG_TYPE.PARTNER, admin_const.E_IMG_THUMB.NO_THUMB),
                link = item_view.link,
                date_modified = Udate_time.GetCurrentDateTime(),
                language_id = item_view.language_id,
                order_display = item_view.order_display,
                keyword = item_view.keyword,
                description = item_view.description,
                is_vip = item_view.is_vip 
            };

            int ret_id = partnerModels.adm_partner_update(item);

            if (ret_id > 0)
            {
                ViewBag.result_message = admin_const.UPDATE_OK;
                ViewBag.redirect = "/vi/admPartner/partner_list";
                return PartialView("../Shared/control_redirect");
            }
            else
            {
                ViewBag.result_message = admin_const.UPDATE_FAIL;
                ViewBag.result = false;
                return PartialView("../Shared/partner/edit_partner", item);
            }
        }
        //tao ra link ao 
        public ActionResult getlink(string id)
        {
            string text = id;
            string outStr = Ustring.RemoveIllegalCharacters(id);
            return Json(outStr);
        }
        #endregion
    }
}